//
//  firstPageViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "firstPageViewController.h"
#import "FMCollectionViewCell.h"
#import "xwzAFNetTool.h"
#import "FMListModel.h"
#import "tListRadioModel.h"
#import "FMListDatailViewController.h"
#import "classifyViewController.h"
#define VIDEO_URL @"http://c.m.163.com/nc/video/home/0-10.html"
// FM -1.13
#import "FMPlayViewController.h"

@interface firstPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, getSidDelegate>

@end
@interface firstPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, FMcolectionCellDelegate>


@end
@interface firstPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,LolVideoVCDelegate>
@property (nonatomic , strong) NSMutableArray *lolArr;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowL;
#pragma FM数组
@property (nonatomic, strong) NSMutableArray *FMarr;

@property (nonatomic, strong) NSMutableArray *classifyArr;
@property (nonatomic, strong) NSMutableArray *videoArr;
#pragma FM全局button
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageV;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic , strong) MBProgressHUD  *hub;


@end

@implementation firstPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
#pragma 设置navigationBar颜色
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    self.navigationItem.title = @"视 听";
    [self createView];
    [self requestData];
    [self getFMData];
    [self createShowButtonAlways];


}

- (void)LolVideoVCDelegate:(NSInteger)index
{
    
    LolViewController *lolVC = [[LolViewController alloc]init];
    LolModel *lolModel   = [self.lolArr objectAtIndex:index];
    lolVC.roomID = lolModel.uid;
    lolVC.titleRoom = lolModel.title;
    
    [self presentViewController:lolVC animated:YES completion:^{
        
    }];
}

- (void)createView
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self createCollectionView];
}

- (void)refachData
{
    [self requestData];
}

- (void)requestData
{
    [xwzAFNetTool getNetWithURL:@"http://www.quanmin.tv/json/play/list.json?01121359" body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        self.lolArr = [NSMutableArray array];
        
        self.lolArr = [LolModel  baseModelWithArr:arr];
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)createCollectionView
{
    self.flowL = [[UICollectionViewFlowLayout alloc] init];
    self.flowL.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    self.flowL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:self.flowL];
    self.flowL.minimumInteritemSpacing = 0;
    self.flowL.minimumLineSpacing = 0;
    self.collectionView.scrollEnabled = YES;
    
    [self.collectionView registerClass:[videoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.collectionView registerClass:[FMCollectionViewCell class] forCellWithReuseIdentifier:@"cellFM"];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[LoLVideoCollectionViewCell class] forCellWithReuseIdentifier:@"lolVideoCell"];
    
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.dataSource = self;
    
    
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (0 == indexPath.row) {
        videoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.classifyArr) {
            cell.classifyArr = self.classifyArr;
            [cell.hub hide:YES];
        }
        if (self.videoArr) {
             cell.videoArr = self.videoArr;
        }
#pragma 切换页面时停止播放
        [[avTools sharePlayer] pause];
        cell.backgroundColor = [UIColor cyanColor];
        return cell;
        
    } else if (1 == indexPath.row) {
        
        FMCollectionViewCell *cellF = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellFM" forIndexPath:indexPath];
        cellF.delegate = self;
        cellF.arr = self.FMarr;
#pragma 切换页面时停止播放
        [[avTools sharePlayer] pause];
        return cellF;
        
    }

    LoLVideoCollectionViewCell *lolVideoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lolVideoCell" forIndexPath:indexPath];
    lolVideoCell.delegate = self;
    lolVideoCell.lolArr = self.lolArr;
        lolVideoCell.backgroundColor = [UIColor whiteColor];
        return lolVideoCell;

}

#pragma xn下拉
- (void)refreshData
{
    [self createData];
}

#pragma xn上拉
- (void)getMore
{
    self.page = self.page + 10;
    self.urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld-10.html", self.page];
    [self getMoreDataWithURL:self.urlStr];
}

- (void)getMoreDataWithURL:(NSString *)url
{
    [xwzAFNetTool getNetWithURL:url body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = [responseObject objectForKey:@"videoSidList"];
        for (NSMutableDictionary *dic in arr) {
            videoModel *vModel = [[videoModel alloc] init];
            [vModel setValuesForKeysWithDictionary:dic];
            [self.classifyArr addObject:vModel];
        }
        
        
        NSMutableArray *videoListArr = [responseObject objectForKey:@"videoList"];
        for (NSMutableDictionary *dic in videoListArr) {
            videoModel *vModel = [[videoModel alloc] init];
            [vModel setValuesForKeysWithDictionary:dic];
            [self.videoArr addObject:vModel];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)createData
{
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hub.labelText = @"加载中....";
    [xwzAFNetTool getNetWithURL:VIDEO_URL body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = [responseObject objectForKey:@"videoSidList"];
        self.classifyArr = [videoModel baseModeWithArr:arr];
        if (self.classifyArr) {
            [self.collectionView reloadData];
        }
        
        NSMutableArray *videoArr = [responseObject objectForKey:@"videoList"];
        self.videoArr = [videoModel baseModeWithArr:videoArr];
        if (self.videoArr) {
            [self.collectionView reloadData];
            [self.hub hide:YES];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

// FM数据请求
- (void)getFMData
{
    
    [xwzAFNetTool getNetWithURL:@"http://c.m.163.com/nc/topicset/android/radio/index.html" body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = [responseObject objectForKey:@"cList"];
        self.FMarr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in arr) {
            FMListModel *listModel =[[FMListModel alloc] initWithDic:tempDic];
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSMutableDictionary *tempListDic in [tempDic objectForKey:@"tList"]) {
                tListRadioModel *tlistModel = [[tListRadioModel alloc] initWithDic:[tempListDic objectForKey:@"radio"]];
                tlistModel.tid = [tempListDic objectForKey:@"tid"];
                [tempArr addObject:tlistModel];
            }
            
            listModel.tList = tempArr;
            [self.FMarr addObject:listModel];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"FM数据请求失败");
        
    }];
    
    
}
#pragma 得到sid跳转页面

- (void)getSid:(NSString *)sid
{
    classifyViewController *classifyVC = [[classifyViewController alloc] init];
    
    classifyVC.sid = sid;
    [self.navigationController pushViewController:classifyVC animated:YES];
    
}

#pragma FM协议跳转1.13 - 列表

// FM 协议跳转FM列表
- (void)returnToListDateilByIndex:(NSInteger) index
{
    FMListDatailViewController *listDateilVC = [[FMListDatailViewController alloc] init];
    listDateilVC.cid = [[self.FMarr objectAtIndex:index] cid];
    [self.navigationController pushViewController:listDateilVC animated:YES];
}

#pragma FM协议跳转1.13 - play
- (void)playByRow:(NSInteger)row index:(NSInteger)index
{
    NSLog(@"vc %ld %ld", row, index);
    
    FMPlayViewController *playVC = [FMPlayViewController sharePlayerViewController];
    NSLog(@"%@", [[[[self.FMarr objectAtIndex:index] tList] objectAtIndex:row] tid]);
    playVC.tid = [[[[self.FMarr objectAtIndex:index] tList] objectAtIndex:row] tid];
    playVC.docid = [[[[self.FMarr objectAtIndex:index] tList] objectAtIndex:row] docid];
       [playVC.player stop];
    playVC.indexLast = 0;
    playVC.indexNext = 0;
    [self.navigationController pushViewController:playVC animated:YES];
}

#pragma 全局显示button

- (void)createShowButtonAlways
{
    NSArray *NameArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"audionews_playlist_playing01"], [UIImage imageNamed:@"audionews_playlist_playing02"], nil];
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40, 200, 30, 30)];
    self.imageV.backgroundColor = [UIColor whiteColor];
    self.imageV.animationImages = NameArr;
    [self.navigationController.view addSubview:self.imageV];
    self.imageV.animationDuration = 0.5;
    self.imageV.hidden = YES;
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];

    self.button.frame = CGRectMake(self.view.frame.size.width - 40, 200, 30, 30);
    [self.button addTarget:self action:@selector(buttonAlwaysAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button.hidden = YES;
    [self.navigationController.view addSubview:self.button];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notAction) name:@"play" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction) name:@"stop" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayActon) name:@"stopFM" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayActon) name:@"stopMusic" object:nil];
}

- (void)buttonAlwaysAction:(UIButton *)sender
{
    FMPlayViewController *playVC = [FMPlayViewController sharePlayerViewController];
    [self.navigationController pushViewController:playVC animated:YES];
}

- (void)notAction
{
    self.button.hidden = NO;
    self.imageV.hidden = NO;
    [self.imageV startAnimating];
    
}
- (void)stopAction
{
    self.button.hidden = YES;
    self.imageV.hidden = YES;
    
}

- (void)stopPlayActon
{
    NSLog(@"6666");
    FMPlayViewController *playVC = [FMPlayViewController sharePlayerViewController];
    [playVC.player pause];
    self.button.hidden = YES;
    self.imageV.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
